const db = require("../../models/index")
const Swiper = db.swipers

// 查询所有数据
exports.findAll = (req, res) => {
    Swiper.findAll().then((data) => {
        res.send({
            data: data,
            code: 101,
            msg: "查询成功"
        })
    }).catch(err => {
        console.log(err.message + "查询数据出错")
    })
}

exports.create = (req, res) => {
    const dataTable = {
        swiper_title: req.body.swiper_title,
        swiper_type: req.body.swiper_type,
        swiper_text: req.body.swiper_text,
        URL: req.body.URL
    }

    Swiper.create(dataTable).then(data => {
        if (data) {
            res.send({data: data, code: 104, message: "注册成功"})
            console.log(data)
        } else {
            res.send({data: null, code: 204, message: "注册失败"})
        }
    }).catch(err => {
        console.log(err.message + "创建数据出错")
    })
}

// 通过id删除数据
exports.delete = (req, res) => {

    Swiper.destroy({
        where: {
            id: req.body.id
        }
    }).then(data => {
        res.send({
            data: data,
            message: "删除成功",
            code: 102
        })
    }).catch(err => {
        res.send({
            data: null,
            message: "删除失败",
            code: 202
        })
    })
}